<?php

class Process {
	
	private $post = array();
	
	public function __construct(){
	
	}
	
	public function setPost( $post ){
		$this->post = $post;
	}
	
	public function getPost(){
		return $this->post;
	}
	
	public function SaveCreate(){

		
		$query = ' INSERT INTO books SET   ';
		$query .= ' date_id="'.strtotime($this->getPost()['date_id']).'" , ';
		$query .= ' blance="'.$this->getPost()['blance'].'" , ';
		$query .= ' memo="'.$this->getPost()['memo'].'" , ';
		$query .= ' user="'.$this->getPost()['user'].'" , ';
		$query .= ' money="'.$this->getPost()['money'].'" , ';
		$query .= ' create_time="'.time().'" , ';
		$query .= ' update_time="'.time().'"  ';

		
		$res = mysql_query( $query );
		if( $res ){
			echo 1;
		}else{
			echo 0;
		}
	}
	
	
	
	public function SaveEdit(){
		$query = ' UPDATE books SET   ';
		$query .= ' date_id="'.strtotime($this->getPost()['date_id']).'" , ';
		$query .= ' blance="'.$this->getPost()['blance'].'" , ';
		$query .= ' memo="'.$this->getPost()['memo'].'" , ';
		$query .= ' user="'.$this->getPost()['user'].'" , ';
		$query .= ' money="'.$this->getPost()['money'].'" , ';
		
		$query .= ' update_time="'.time().'"  ';
		$query .= ' WHERE 1 AND id = "'.$this->getPost()['id'].'" ';
		
		
		$res = mysql_query( $query );
		if( $res ){
			echo 1;
		}else{
			echo 0;
		}
	}

	public function Login(){
		$query = ' SELECT * FROM member WHERE 1 AND account="'.$this->getPost()['account'].'" AND password="'.md5( $this->getPost()['password'] ).'" ';
		$res = mysql_query( $query );
		$cnt = mysql_num_rows($res);
		if( $cnt >０ ){
			session_start();
			$row = mysql_fetch_assoc( $res );
			$_SESSION['account'] = $row;
			echo '
				<script>
					alert("'.$row['username'].'님 로그인 하셨습니다.");
					window.location.href="./list.php";
				</script>
			';
			exit;
		}else{
			echo '
				<script>
					alert("아이디 혹은 비밀번호가 올바르지 않습니다.");
					window.history.back();
				</script>
			';
			exit;
		}
	}

	public function Logout(){
		unset($_SESSION['account']);
		header("Location:index.php");
	}
	
	public function DelList(){
		$REQUEST = $this->getPost();
		$query = ' DELETE FROM books WHERE 1 AND id in ( '.$REQUEST['id'].' ) ';
		$res = mysql_query($query);
		
		if( $res ){
			echo '
				<script>
					alert("선택한 내용이 삭제 되였습니다.");
					window.location.href="./list.php";
				</script>
			';
			exit;
		}else{
			echo '
				<script>
					alert("선택한 내용이 삭제 실패하셨습니다.");
					window.history.back();
				</script>
			';
			exit;
		}
	}
	
}